package service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;

import org.hibernate.Hibernate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.telkomsigma.hibernate.persistence.photo.entity.Photo;
import com.telkomsigma.hibernate.services.photo.PhotoService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class TestPhotoService extends AbstractTransactionalJUnit4SpringContextTests{
	
	@Autowired
	private PhotoService photoService;	

	@Test
	public void testCheckDaoConfig() {				
		assertNotNull(photoService);		
	}
	
	@Test
	public void testFindAllPhoto(){		
		List<Photo> photoList = photoService.findAll(0,20);
		assertEquals(1, photoList.size());
	}
	
	@Test
	public void testFindById(){
		Photo result = photoService.findById("402880ea319449f001319449f44d0001");
		assertNotNull(result);
	}
	
	@Test
	public void testInsertPhoto() throws IOException{			
		File file = new File("E:/Penguins.jpg");		
		InputStream is = new BufferedInputStream(new FileInputStream(file));
		Blob blob = Hibernate.createBlob(is);
		Photo photo = new Photo();
		photo.setPhoto(blob);		
		Photo result = photoService.insert(photo);
		assertNotNull(result);
	}	
		
	@Test
	public void testRemovePhoto(){
		Photo photo = new Photo();
		photo.setId("402880ea319449f001319449f44d0001");
		boolean check = photoService.remove(photo);
		assertTrue(check == true);
	}	
}
